const jnpf = {
  goBack() {
    uni.navigateBack();
  },
  toDecimal(num) {
    if (num == null) {
      num = "0";
    }
    num = num.toString().replace(/\$|\,/g, "");
    if (isNaN(num)) num = "0";
    var sign = num == (num = Math.abs(num));
    num = Math.floor(num * 100 + 0.50000000001);
    var cents = num % 100;
    num = Math.floor(num / 100).toString();
    if (cents < 10) cents = "0" + cents;
    for (var i = 0; i < Math.floor((num.length - (1 + i)) / 3); i++)
      num =
        num.substring(0, num.length - (4 * i + 3)) +
        "" +
        num.substring(num.length - (4 * i + 3));
    return (sign ? "" : "-") + num + "." + cents;
  },
  getScriptFunc(str) {
    let func = null;
    try {
      func = eval(str);
      return func;
    } catch (error) {
      console.log(error);
      return false;
    }
  },
  isEmpty(data) {
    return data === null || data === undefined || data === "";
  },
  interfaceDataHandler(data) {
    if (!data.dataProcessing) return data.data;
    const dataHandler = this.getScriptFunc(data.dataProcessing);
    if (!dataHandler) return data.data;
    return dataHandler(data.data);
  },
  // 基于dayjs日期格式化,时间戳(毫秒)转日期
  getCurrentDate(format = "YYYY-MM-DD") {
    const now = new Date(); // 获取当前日期和时间
    const year = now.getFullYear(); // 获取年份
    const month = now.getMonth() + 1; // 获取月份，月份从0开始，所以需要加1
    const day = now.getDate(); // 获取日期
    // 将月份和日期格式化为两位数
    const formattedMonth = month < 10 ? "0" + month : month;
    const formattedDay = day < 10 ? "0" + day : day;
    // 替换格式字符串中的占位符
    return format
      .replace("YYYY", year)
      .replace("MM", formattedMonth)
      .replace("DD", formattedDay);
  },
  capAmountToString(values) {
    if (values === null || values === "") {
      return "";
    }
    values = values.toString();
    let reg = new RegExp(",");
    values = values.replace(reg, "");
    // 不足两位小数补零
    let capArr = values.split(".");
    if (capArr.length < 2) {
      capArr[1] = "00";
    } else if (capArr[1].length == 1) {
      capArr[1] = capArr[1][0] + "0";
    }

    let len = capArr[0].length; //整数长度
    let len2 = capArr[1].length; //小数长度
    let arr = [],
      arr2 = [];
    let chin_list = [
      "零",
      "壹",
      "贰",
      "叁",
      "肆",
      "伍",
      "陆",
      "柒",
      "捌",
      "玖",
    ]; //所有的数值对应的汉字
    let chin_lisp = [
      "仟",
      "佰",
      "拾",
      "兆",
      "仟",
      "佰",
      "拾",
      "亿",
      "仟",
      "佰",
      "拾",
      "万",
      "仟",
      "佰",
      "拾",
    ]; //进制
    let chin_lisp2 = ["角", "分"]; //进制

    // 数字转大写，添加进制
    for (let i = 0; i < len; i++) {
      arr.push(parseInt(capArr[0][i])); //输入的数据按下标存进去   存进去的只是数字
      arr[i] = chin_list[arr[i]]; //是根据我们输入的输入的数字，对应着我们的chin_list这个数组
    }
    for (let i = len - 1, j = 1; i > 0; i--) {
      //i =2	1		//倒序		为了添加进制，方便我们去观看
      arr.splice(i, 0, chin_lisp[chin_lisp.length - j++]); //j=2
    }
    for (let n = 0; n < len2; n++) {
      arr2.push(parseInt(capArr[1][n])); //输入的数据按下标存进去   存进去的只是数字
      arr2[n] = chin_list[arr2[n]]; //是根据我们输入的输入的数字，对应着我们的chin_list这个数组
    }
    for (let i = len2, j = 1; i > 0; i--) {
      //i =2	1		//倒序		为了添加进制，方便我们去观看
      arr2.splice(i, 0, chin_lisp2[chin_lisp2.length - j++]); //j=2
    }

    arr = arr.join("");
    // 整数位处理
    if (len >= 1) {
      arr += "元";
    } //1234510001=>壹拾贰亿叁仟肆佰伍拾壹万零壹元
    arr = arr.replace(/零[仟佰拾]/g, "零"); //100051231 =>壹亿零零零伍万壹仟贰佰叁拾壹
    arr = arr.replace(/零{2,}/g, "零"); //壹亿零零零伍万壹仟贰佰叁拾壹  =>壹亿零伍万壹仟贰佰叁拾壹
    arr = arr.replace(/零([兆|亿|万|元])/g, "$1"); //12300000壹仟贰佰叁拾零万零元 =>壹仟贰佰叁拾万元
    arr = arr.replace(/亿零{0,3}万/, "亿"); //10000000123 =>壹佰亿万零壹佰贰拾叁元
    arr = arr.replace(/兆零{0,3}亿/, "兆"); //10000000000123 壹拾零兆亿零壹佰贰拾叁元 =>
    arr = arr.replace(/^元/, "零元"); //0 ->零元

    // 小数位处理
    arr2 = arr2.join("");
    arr = arr + arr2;
    arr = arr.replace(/零角/, "零"); //11.01 壹拾壹元零角壹分 =>壹拾壹元零壹分
    arr = arr.replace(/零{1,2}分/, "整"); //

    return arr;
  },
  // =======================以下是消息推送方法
  getAllMessage() {
    return plus.push.getAllMessage();
  },
  changeBadge(number) {
    // console.log("number", number);
    // console.log("触发了更新角标");
    // const messages = this.getAllMessage();
    // console.log("messages", messages.length);
    // console.log("messages", messages);
    // plus.runtime.setBadgeNumber(number || messages.length);
  },
  /**
		 * @param {string} content 消息内容
		 * @param {Object} payload 消息承载的数据
		 * @param {Object} options 消息配置
		 * @param {Boolean} isChange 创建消息后是否更新BadgeNumber
		 * @example 
		 * 	this.$push.create("点击查看消息内容>>", {url: "/pages/xxxx/xxx"}, {
				delay: 3,
				title: "您有3条新的消息",
			});
		 */
  create(content, payload = {}, options, isChange, num) {
    plus.push.createMessage(...arguments);
    // this.changeBadge(num);
    // const delay = options.delay ? ~~options.delay * 1000 : 0;
    // setTimeout(() => {
    //   isChange && this.changeBadge(num);
    // }, delay);
  },
  /**
   * @param {number} message
   * @description
   * 		- 传入message时，删除消息列表中的message；
   *      - 不传参数：则全部清空
   */
  remove(message, isChange) {
    message ? plus.push.remove(message) : plus.push.clear();
    // isChange && this.changeBadge();
  },
  /**
   * @description 需要在App.vue中onLaunch中调用
   */
  init() {
    // console.log("初始化推送");
    plus.push.setAutoNotification(true); // setAutoNotification 默认就是true
    // 监听消息接收事件
    plus.push.addEventListener("receive", (message) => {
      console.log("收到消息", message);
      // 处理接收到的消息
      const payload = message.payload || {};
      if (payload.url) {
        // 你可以在后台显示通知或处理其他逻辑
        console.log("消息包含URL:", payload.url);
      } else {
        console.log("消息不包含URL");
      }
    });
    // 监听消息点击事件
    plus.push.addEventListener("click", (message) => {
      console.log("监听消息点击事件", message);
      let { url } = message.payload;
      console.log("url", url);
      if (url) {
        console.log("有url", url);
        uni.navigateTo({
          url,
          success: () => {
            this.remove(message, true);
          }, // 可以根据需求删除消息 this.remove(message);
          complete: () => {}, // 可以根据需求删除消息 this.remove(message);
        });
      } else {
        console.log("没有url");
      }
    });
  },
};
export default jnpf;
